package com.team1.supervend.domain;

public class SaleHandler {
	private AccountingSystem        currentAccountingSystem;
	private CentralMonitoringSite   currentCentralMonitoringSite;
	private PaymentHandler          currentPaymentHandler;
	private PurchaseOrder           currentPurchaseOrder;
	private VendingComponent        currentVendingComponent;
	private ReceiptPrinter          currentReceiptPrinter;
	float                           returnBalance;
	
	public SaleHandler(){
		//System.out.println("Creating Sale Handler");
		createPaymentHandler();
		requestPrePayment();
		makeNewPurchaseOrder();
		makeNewVendingComponent();
		makeNewReceiptPrinter();
		makeNewAccountingSystem();
		makeNewCentralMonitoringSite();
		
	}

	public void requestPrePayment(){
		System.out.println(" ");
		System.out.println("------------------------------ ");
		System.out.println("Please Insert Payment To Begin");
		System.out.println("------------------------------ ");
		System.out.println(" ");
	}
	
	public void makeNewVendingComponent(){
		currentVendingComponent = new VendingComponent();
	}
	
	public VendingComponent getCurrentVendingComponent(){
		return currentVendingComponent;
	}
	
	public void makeNewReceiptPrinter(){
		currentReceiptPrinter = new ReceiptPrinter();
	}
	
	public ReceiptPrinter getCurrentReceiptPrinter(){
		return currentReceiptPrinter;
	}
	
	public void makeNewAccountingSystem(){
		currentAccountingSystem = new AccountingSystem();
	}
	
	public AccountingSystem getCurrentAccountingSystem(){
		return currentAccountingSystem;
	}

	public void makeNewCentralMonitoringSite(){
		currentCentralMonitoringSite = new CentralMonitoringSite();
	}
	
	public CentralMonitoringSite getCurrentCentralMonitoringSite(){
		return currentCentralMonitoringSite;
	}
	
	public void createPaymentHandler(){
		currentPaymentHandler = new PaymentHandler();
	}
	
	public PaymentHandler getPaymentHandler(){
		//System.out.println("Returning Existing Payment Handler");
		return currentPaymentHandler;
	}
	
	public boolean preValidateCashPayement(){
		boolean retval = true;
		return  retval;
	} 
	
	public void makeNewPurchaseOrder()
	{
		currentPurchaseOrder = new PurchaseOrder();
	}

	public PurchaseOrder getCurrentPurchaseOrder()
    {
		//System.out.println("Returning Existing Purchase Order");
    	return currentPurchaseOrder;
    }
	
	public Boolean completeSale(){
		Boolean isComplete = false;
		System.out.println(" ");
		System.out.println("----------------");
		System.out.format ("Balance due = $%.2f", currentPurchaseOrder.getBalance());
		System.out.println("");
		System.out.println("----------------");
		System.out.println(" ");
		if (currentPurchaseOrder.getBalance() > 0.0f){
			currentPurchaseOrder.becomeComplete();
			returnBalance();
			vendItems();
			printReciept();
			System.out.println("logging sale");
			currentAccountingSystem.getAccountingAdapter().logSale(currentPurchaseOrder);
			isComplete = true;
			
			
		}
		else if (currentPurchaseOrder.getBalance() == 0.0f){
			currentPurchaseOrder.becomeComplete();
			vendItems();
			printReciept();
			System.out.println("logging sale");
			currentAccountingSystem.getAccountingAdapter().logSale(currentPurchaseOrder);
			isComplete = true;
		}
		else{
			System.out.println(" ");
			System.out.println("----------------");
			System.out.println("Please Add Payment To Complete Sale");
			System.out.println("----------------");
			System.out.println(" ");
			isComplete = false;
		}
		return isComplete;	
			
	}
	//send commands to vend items
	public void vendItems(){
		System.out.println("");
		System.out.println("----------------");
		System.out.println("Vending Items List");
		System.out.println("----------------");
		System.out.println("");
		for (int i=0; i < currentPurchaseOrder.selectedItems.size(); i++){
			SelectedItem item = currentPurchaseOrder.selectedItems.get(i);
			//String s = item.description.getDescription();
			getCurrentVendingComponent().vendItem(item);
		}
		System.out.println("");
		System.out.println("----------------");
		System.out.println("End of Vending Items List");
		System.out.println("----------------");
		System.out.println("");
	}
	
	public void printReciept(){
		System.out.println(" ");
		System.out.println("----------------");
		System.out.println("Printing Reciept");
		System.out.println("----------------");
		System.out.println(" ");
		for (int i=0; i < currentPurchaseOrder.selectedItems.size(); i++){
			SelectedItem item = currentPurchaseOrder.selectedItems.get(i);
			getCurrentReceiptPrinter().printReciept(item);	
		}
		getCurrentReceiptPrinter().receiptTotals();
		System.out.println(" ");
		System.out.println("----------------");
		System.out.println("End of Reciept");
		System.out.println("----------------");
		System.out.println(" ");
	}
	
	public void returnBalance(){
		returnBalance = currentPurchaseOrder.getBalance();
		System.out.println(" ");
		System.out.println("----------------");
		System.out.format("Please check change tray for remaining balance of: $%.2f\n", returnBalance);
		System.out.println("----------------");
		System.out.println(" ");
	}
	
	public float getBalance(){
		float balance = 0.0f;
		System.out.println("final balance");
		System.out.println(balance);
		balance = currentPurchaseOrder.getBalance();
		return balance;
	}
	

}
